package UI
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * 面板类
	 * @author jianhong
	 */ 
	public class Panel extends Sprite
	{
		private	var _selected:Boolean = false;
		private var _lastMousePt:Point = new Point();
		private	var _bgImg:Bitmap = null;

		public function Panel(bgImg:Bitmap)
		{
			super();
			this.addChild(bgImg);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			this._bgImg = bgImg;
		}
		
		public	function	onMouseDown(evt:MouseEvent):void{
			var pixel:uint = this._bgImg.bitmapData.getPixel32(this.mouseX, this.mouseY);
			pixel = pixel >> 24;
			if (pixel == 0)
				return;
			this._selected = true;
			this._lastMousePt.x = this.mouseX;
			this._lastMousePt.y = this.mouseY;
			this._lastMousePt = this.localToGlobal(this._lastMousePt);			

			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		public	function	onMouseMove(evt:MouseEvent):void{
			if (this._selected == false)
				return;
			
			var pt:Point = new Point(this.mouseX, this.mouseY);
			pt = this.localToGlobal(pt);
			var xDis:Number = pt.x - this._lastMousePt.x;
			var yDis:Number = pt.y - this._lastMousePt.y;
			this.x += xDis;
			this.y += yDis;
			
			this._lastMousePt = pt;
		}
		
		public	function	onMouseUp(evt:MouseEvent):void{
			this._selected = false;
		}
		
		public	function	onMouseOut(evt:MouseEvent):void{
			this._selected = false;
		}
	}
}